/**
 * Created by Diego on 16/3/8.
 */
$(function(){
//    $(".reply").each(function(){
//        $(this).click(function(){
//            //$(".ql_comment_form").hide();
//            $(this).parent().parent().parent().siblings().find(".ql_comment_form").hide();
//            $(this).parent().siblings(".ql_comment_form").toggle();
//        })
//    })
////    取消评论
//    $(".ql_command_cancel").click(function(){
//        $(this).parent().parent(".ql_comment_form").toggle();
//    })
    //care me?..
    $(".cereforyou").each(function(){
        var _this=this;
        $(_this).click(function(){
            var _flag=$(this).attr("data-flag");
            if(_flag=="false")
            {
                $(this).html("取消关注").css("color","gray").attr("data-flag","true");
            }
            else{
                $(this).html("关注他").removeAttr("style").attr("data-flag","false");
            }
        })
    });
    $("#sendTo").bind("click",function(){
        var sendID=$("#dataobj").attr("data-id");
        var sendContent=$("textarea.editor_content").val();
        $.get("http://www.qlcoder.com/api/user/sendmsg",{userid:sendID,content:sendContent},function(data){
            console.log(data);
            if(data.success==true)
            {
                $("textarea.editor_content").val("");
                $('#myModal').modal('hide');
                $(".alert").removeClass("none");
                setTimeout(function(){$(".alert").addClass("none")},1000)
            }
        })
    });
    //加载最新动态内容
    $(".user_task a.home").trigger('click');
    //加载更多
    $(".get-more a").click(function(){
        var page=$(this).parent().attr("data-page");
        var id=$(this).parent().attr("data-id");
        var name=$(this).parent().attr("data-name");
        $(this).parent().attr("data-flag",false);
        load_new_dt(page,id,name);
    })
});
//study->学习资料
//offer->贡献题目
//question->提问
//answer->回答
function getRequest(str,obj){
    var _str=str;
    var ID=$(obj).parent().attr("data-id");
    var name=$(obj).parent().attr("data-name");
    var html="";
    $(".loadding_img").removeClass("none");
    if(_str=="offer")//贡献题目
    {
        $(".get-more").hide();
        $(".user_sec_head").html('<span class="task_author pd_r10 bold">'+name+'</span>贡献的题目');
        $.get("http://www.qlcoder.com/api/user/gettasks",{userid:ID},function(data){
            if(data.length!=0)
            {
                for(var i=0;i<data.length;i++)
                {
                    var content=data[i].content;
                    var removeTag=content.replace(/<\/?.+?>/g,"");
                    var removeTag1=removeTag.replace(/&nbsp;/g,"");
                    if(removeTag1.length>180)
                    {
                        removeTag1=removeTag1.substr(0,180)+'...<a href="/task/'+data[i].url+'" target="_blank">显示全部</a>';
                    }
                    html+= '<!-- task相关 S--> <div class="task_item follow_item"> <div class="task_title"><a class="font20 fontw" href="/task/'+data[i].url+'" target="_blank">'+data[i].title+'</a></div>'+
                        '<div class="task_content pd_t10 pd_b10 "> <div class="fl task_content_detail_con">'+
                        ' <div class="font15 ">'+
                        '<div class="task_content_detail"><a href="/task/'+data[i].url+'" style="color:black" target="_blank">'+removeTag1+'</a></div>'+
                        '<p class="task_time" style="margin-left: 0;"> 发布于'+data[i].created_at+'</p> </div></div> </div> </div><!-- task相关 E--> ';
                }
                toHtml(html);
            }
            else{
                $(".user_contain").html("暂无贡献题目");
            }
            $(".user_task a").removeClass("active");
            $(obj).addClass("active");
            $(".get-more").attr("data-flag","true");
            $(".loadding_img").addClass("none");
        })
    }
    else if(_str=="index"){
        load_new_dt(1,ID,name);
        $(obj).addClass("active");
    }
    else if(_str==="question"){
        $(".get-more").hide();
        $(".user_sec_head").html('<span class="task_author pd_r10 bold">'+name+'</span>的提问');
        $.get("http://www.qlcoder.com/api/user/getdiss",{userid:ID},function(data){
            if(data.length!=0)
            {
                
                for(var i=0;i<data.length;i++)
                {
                    html+= '<!-- task相关 S--> <div class="task_item follow_item"> <div class="task_content pd_t10 pd_b10 ">'+
                            '<div class="fl task_content_detail_con"> '+
                            '<span class="qa-view-count"><span class="qa-view-count-data">'+data[i].vote+'</span><span class="qa-view-count-pad"> 浏览</span></span>'+
                            '<div class="font20 fontw"> <a class="" href="/q/'+data[i].url+'">'+data[i].title+'</a>'+
                            '<p class="task_time mr0"> 于 '+data[i].created_at+' 提问</p>'+
                            '</div> </div> </div> </div> <!-- task相关 E-->';
                }
                toHtml(html);
            }
            else{
                $(".user_contain").html("暂无提问");
            }
            $(".user_task a").removeClass("active");
            $(obj).addClass("active");
            $(".get-more").attr("data-flag","true");
            $(".loadding_img").addClass("none");
        })
    }
    else if(_str==='answer'){
        $(".get-more").hide();
        $(".user_sec_head").html('<span class="task_author pd_r10 bold">'+name+'</span>的回答');
        $.get("http://www.qlcoder.com/api/user/getstudies",{userid:ID},function(data){
            if(data.length!=0)
            {
                
                for(var i=0;i<data.length;i++)
                {
                    var content=data[i].comment;
                    var removeTag=content.replace(/<\/?.+?>/g,"");
                    var removeTag1=removeTag.replace(/&nbsp;/g,"");
                    if(removeTag1.length>180)
                    {
                        removeTag1=removeTag1.substr(0,180)+'...<a href="/q/'+data[i].q.url+'/a/'+data[i].url+'" target="_blank">显示全部</a>';
                    }
                    html+= '<div class="task_item follow_item"> <div class="task_title"><a class="font18 fontw" href="/q/'+data[i].q.url+'/a/'+data[i].url+'" target="_blank">'+data[i].q.title+'</a></div>'+
                        '<div class="task_content pd_t10 pd_b10 "> <div class="fl task_content_detail_con"> <div class="task_vote fl"><!-- 点赞 --> <button class="up mr_b10" data-statu="false" title="赞同"><i class="vote_arrow"></i>'+
                        '<span class="count" data-count="'+data[i].vote+'">'+data[i].vote+'</span> </button> </div> <div class="font15 "> '+
                        '<div class="task_content_detail"><a href="/q/'+data[i].q.url+'/a/'+data[i].url+'" style="color:black" target="blank">'+removeTag1+'</a></div>'+
                        '<p class="task_time"> 于'+data[i].created_at+' 回答</p> </div></div> </div> </div><!-- task相关 E--> '
                }
                toHtml(html);
            }
            else{
                $(".user_contain").html("暂无回答");
            }
            $(".user_task a").removeClass("active");
            $(obj).addClass("active");
            $(".get-more").attr("data-flag","true");
            $(".loadding_img").addClass("none");
        })
    }
    else if(_str==='kuibu'){
        $(".get-more").hide();
        $(".user_sec_head").html('跬步获取记录');
        $.get("http://www.qlcoder.com/api/user/getkuibu",{userid:ID},function(data){
			console.log(data);
            if(data.length!=0)
            {
                html+='<ul class="kb">';
                for(var i=0;i<data.length;i++)
                {
                
                    html+= '<li class=""><div class="fl"><span>'+data[i].time+'</span></div>'+
                    '<div class="fl kb-width-way"><span>'+data[i].type+'</span></div>'+
                    '<div class="fl kb-width-res overhidden"><a href="javascript:;">'+data[i].text+'</a></div>'+
                    '<div class="fr"><span class="badge ">'+data[i].num+'</span></div></li>';
                }
                html+='</ul>';
                toHtml(html);
            }
            else{
                $(".user_contain").html("暂无跬步获取记录");
            }
            $(".user_task a").removeClass("active");
            $(obj).addClass("active");
            $(".get-more").attr("data-flag","true");
            $(".loadding_img").addClass("none");
        })
    }

}
//fill html
function toHtml(html){
    var _html=html;
    $(".user_contain").html("").append(_html);
}


//首页最新动态,新增加载更多  3.16  @diego
function load_new_dt(page,id,name){
    var html="";
    $(".user_sec_head").html('最新动态');
    $.get("http://www.qlcoder.com/api/user/timeline",{page:page,userid:id},function(data){
        if(data.length!=0){
            var _flag=$(".get-more").attr("data-flag");
            console.log(_flag);
            if(_flag=="true"){
                console.log("flag++");
                $(".user_contain").html("");
                $(".get-more").attr("data-flag","false");
            }
            // html=$(".user_contain").html();
            if(data.length==15)
            {
                $(".get-more").show().attr("data-page",parseInt(page)+1).attr("data-id",id).attr("data-name",name);
            }
            else{
                $(".get-more").hide();
            }
            
            for(var i=0;i<data.length;i++){
                //alert(data[i].content);
                switch (data[i].type){
                    case 1://type  1:代表做出题目
                        var _content1=jQuery.parseJSON(data[i].content);
                        html+='<!-- 攻克题目 S --><div class="follow_task follow_item"> <span class="normal">'+name+'</span> <span class="normal">攻克了任务</span>'+
                            '<h4><a href="/task/'+_content1.url+'" target="_blank">'+_content1.title+'</a></h4> <span class="follow_time normal">于 '+_content1.created_at+'</span> </div><!-- 攻克题目 E -->';
                        break;
                    case 2://type  2.贡献题目
                        var _content2=jQuery.parseJSON(data[i].content);
                        var removeTag=_content2.content.replace(/<\/?.+?>/g,"");
                        var removeTag1=removeTag.replace(/&nbsp;/g,"");
                        if(removeTag1.length>180)
                        {
                            removeTag1=removeTag1.substr(0,180)+'...<a href="/task/'+_content2.url+'" target="_blank">显示全部</a>';
                        }
                        //console.log(_content2);
                        html+='<!-- 贡献的题目 S--> <div class="task_offer follow_item"> <span class="normal">'+name+'</span> <span class="normal">贡献了题目</span>'+
                            '<!-- task S--> <div class="task_title"><a class="font20 fontw" href="/task/'+_content2.url+'" target="_blank">'+_content2.title+'</a></div> <div class="task_content pd_b10 ">'+
                            '<div class="fl task_content_detail_con"> <div class="font15 "> <p class="task_content_detail"><a href="/task/'+_content2.url+'" style="color:black;" target="blank">'+removeTag1+'</a></p>'+
                            '<p class="task_time" style="margin-left:0px;"> 发布于 '+_content2.created_at+'</p> </div> </div> </div> <!-- task E--> </div> <!-- 贡献的题目 E-->';
                        break;
                    case 3://type  3.对学习资料点赞

                        break;
                    case 4://type  4.回复学习资料

                        break;
                    case 5://type  5.撰写学习资料
                        var _content3=jQuery.parseJSON(data[i].content);
                        var _removeTag=_content3.content.replace(/<\/?.+?>/g,"");
                        var removeTag2=_removeTag.replace(/&nbsp;/g,"");
                        if(removeTag2.length>180)
                        {
                            removeTag2=removeTag2.substr(0,180)+'...<a href="/task/'+_content3.task.url+'/learn#'+_content3.url+'" target="_blank">显示全部</a>';
                        }
                        html+= '<div class="task_item follow_item"><span class="normal">'+name+'</span> <span class="normal">撰写了学习资料</span> <div class="task_title"><a class="font20 fontw" href="/task/'+_content3.task.url+'" target="_blank">'+_content3.task.title+'</a></div>'+
                            '<div class="task_content pd_t10 pd_b10 "> <div class="fl task_content_detail_con"> <div class="task_vote fl"><!-- 点赞 --> <button class="up mr_b10" data-statu="false" title="赞同"><i class="vote_arrow"></i>'+
                            '<span class="count" data-count="'+_content3.vote+'">'+_content3.vote+'</span> </button> </div> <div class="font15 "> '+
                            '<div class="task_content_detail"><a href="/task/'+_content3.task.url+'/learn#'+_content3.url+'" style="color:black;" target="blank">'+removeTag2+'</a></div>'+
                            '<p class="task_time"> 发布于'+_content3.created_at+'</p> </div></div> </div> </div><!-- task相关 E--> ';
                        break;
                    default :
                        return null;
                }
            }
            $(".user_contain").append(html);


        }
        else{
            $(".user_contain").html("暂无贡献题目");
        }
        $(".user_task a").removeClass("active");
        $(".loadding_img").addClass("none");
    })
}
